package com.rent.appointment.controller;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.rent.appointment.annotation.Perm;
import com.rent.appointment.dto.ApplyRentDto;
import com.rent.appointment.entity.ApplyRent;
import com.rent.appointment.service.ApplyService;
import com.rent.common.entity.ResponseResult;
import com.rent.common.interceptor.UserContext;
import io.swagger.annotations.*;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

@RestController
@Api(tags = "申请接口")
@RequestMapping("/api/apply")
@Validated
public class ApplyController {
    @Autowired
    private ApplyService applyService;

    @ApiOperation(value = "创建申请",httpMethod = "POST")
    @PostMapping("/create")
    public ResponseResult createApply(@RequestBody ApplyRentDto applyRentDto){
        applyService.createApply(applyRentDto);
        return ResponseResult.success("创建申请成功",null);
    }

    @ApiOperation(value = "我的申请列表",httpMethod = "GET")
    @GetMapping("/myList")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "pageNo", value = "分页数", dataType = "Integer",
                    paramType = "query", required = false, defaultValue = "1"),
            @ApiImplicitParam(name = "pageSize", value = "分页大小", dataType = "Integer",
                    paramType = "query", required = false, defaultValue = "10")
    })
    public ResponseResult applyList(Integer pageNo, Integer pageSize){
        Page<ApplyRent> page = new Page<>(pageNo,pageSize);
        LambdaQueryWrapper<ApplyRent> queryWrapper = new LambdaQueryWrapper<>();
        //从UserContext获取当前用户的userid进行查询
        queryWrapper.eq(ApplyRent::getUserId, UserContext.getUserId());
        Page<ApplyRent> list = applyService.getBaseMapper().selectPage(page,queryWrapper);
        return ResponseResult.success("我的申请列表:",list);
    }

    @Perm("landlord")
    @ApiOperation(value = "房东申请列表",httpMethod = "GET")
    @GetMapping("/landlordList")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "pageNo", value = "分页数", dataType = "Integer",
                    paramType = "query", required = false, defaultValue = "1"),
            @ApiImplicitParam(name = "pageSize", value = "分页大小", dataType = "Integer",
                    paramType = "query", required = false, defaultValue = "10")
    })
    public ResponseResult querylandlordList(Integer pageNo, Integer pageSize){
        Page<ApplyRent> page = new Page<>(pageNo,pageSize);
        LambdaQueryWrapper<ApplyRent> queryWrapper = new LambdaQueryWrapper<>();
        //从UserContext获取当前用户的userid进行查询,查询
        queryWrapper.eq(ApplyRent::getLandlordId, UserContext.getUserId());
        Page<ApplyRent> list = applyService.getBaseMapper().selectPage(page,queryWrapper);
        return ResponseResult.success("房东列表如下:",list);
    }

    @ApiOperation(value = "申请详情",httpMethod = "GET")
    @GetMapping("/{id}")
    public ResponseResult applyDetails(@PathVariable Long id){
        ApplyRent details = applyService.getById(id);
        return ResponseResult.success("申请详情如下:",details);
    }

    @Perm("landlord")
    @ApiOperation(value = "处理申请",httpMethod = "PUT")
    @PutMapping("/handle")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "id", value = "申请号码",
                    paramType = "query", required = true),
            @ApiImplicitParam(name = "status", value = "状态（1-同意，2-拒绝）",
                    paramType = "query", required = true)
    })
    public ResponseResult handleApply(Long id, Integer status){
        applyService.handleApply(id,status);
        return ResponseResult.success("处理申请成功!",null);
    }

    @ApiOperation(value = "取消申请",httpMethod = "PUT")
    @PutMapping("/cancel")
    public ResponseResult cancelApply(@RequestParam Long id){
        applyService.deleteRent(id);
        return ResponseResult.success("取消申请成功!",null);
    }

}
